<?php
namespace app\index\controller;

use think\Db;
use app\index\model\Product;
use app\index\model\Article;
class Index extends Base
{
    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $about = $this->getVal('about_us');
        $img = $this->getVal('company_img');
        $phone = $this->getVal('telephone');
        $person = $this->getVal('person');
        $email = $this->getVal('email');
        $postcode = $this->getVal('postcode');
        $address = $this->getVal('address');
        $logo = $this->getVal('logo');
        $product = new Product();
        $pro_cate = $product->getCate(array('cate_status'=>1,'cate_pid'=>0),'cate_id,cate_name');
        $pro_info = $product->randSel(array('product_status'=>1),'product_id,product_name,product_img');
        $article = new Article();
        $art_info = $article->getList(array('article_status'=>1),'article_id,article_title,article_edit_time','article_add_time desc',7);
        if($art_info){
            foreach ($art_info as &$v){
                $v['article_time'] = date("Y-m-d",$v['article_edit_time']);
            }
        }
        $this->assign('menu',json_encode($this->m));
        $this->assign('mark','home');
        $this->assign('article',$art_info);
        $this->assign('product',$pro_info);
        $this->assign('pro_cate',$pro_cate);
        $this->assign('logo',$logo);
        $this->assign('email',$email);
        $this->assign('postcode',$postcode);
        $this->assign('address',$address);
        $this->assign('person',$person);
        $this->assign('company_img',$img);
        $this->assign('phone',$phone);
        $this->assign('about',$about);
        return $this->fetch('index');
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInfo(){
        $logo = $this->getVal('logo');
        $phone = $this->getVal('telephone');
        $banner = unserialize($this->getVal('banner_img'));
        $data['logo'] = $logo;
        $data['phone'] = $phone;
        $data['banner'] = $banner;
        ajaxReturn($data,'返回成功!',1);
    }

    public function getBottom(){
        $phone = $this->getVal('telephone');
        $copyright = $this->getVal('copyright');
        $logo = $this->getVal('logo');
        $link = unserialize($this->getVal('link'));
        $data['phone'] = $phone;
        $data['copyright'] = $copyright;
        $data['link'] = $link;
        $data['logo'] = $logo;
        ajaxReturn($data,'返回成功!',1);
    }
    public function contact(){
        if(request()->isPost()){
            $data = input('post.');
            if(!$data['name']){
                ajaxReturn(0,'联系人不能为空！',0);
            }
            if(!preg_match('/1[356789]\d{9}/',$data['tel'])){
                ajaxReturn(0,'手机格式不正确！',0);
            }
            if(!$data['content']){
                ajaxReturn(0,'内容不能为空！',0);
            }
            $insert['mess_content'] = $data['content'];
            $insert['mess_name'] = $data['name'];
            $insert['mess_phone'] = $data['tel'];
            $insert['mess_time'] = time();
            $res = Db::name('mess')->insert($insert);
            if($res){
                ajaxReturn(1,'留言成功！',1);
            }else{
                ajaxReturn(0,'留言失败！',0);
            }
        }
        $img = '/baibu/public/static/image/index/t4.png';
        $desc = '联系我们';
        $location = '百步园艺';
        $mark = 'home';
        $this->assign('img',$img);
        $this->assign('desc',$desc);
        $this->assign('location',$location);
        $this->assign('menu',json_encode($this->m));
        $this->assign('mark',$mark);
        return $this->fetch('contact');
    }
}
